package service.impl;

import dao.DeptDao;
import dao.UserDao;
import model.Dept;
import model.User;
import pojo.PageEntity;
import service.UserService;
import utils.PageUtil;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDao(User.class);
    private DeptDao deptDao = new DeptDao(Dept.class);
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public User findByName(String username) {
        return userDao.findByName(username);
    }

    @Override
    public int save(User user) {
        return userDao.save(user);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }


    /**
     * 分页查询用户
     * @param currPage
     * @param realName
     * @return
     */
    @Override
    public PageEntity<User> search(Integer currPage, String realName) {

        PageEntity<User> pageModel = new PageEntity<>();
        Integer pageSize = pageModel.getPageSize();
        // 当前页数据
        List<User> data = userDao.search((currPage - 1) * pageSize, pageSize, realName);

        Integer totalCount;
        //数据条数
        if(realName != null){
            totalCount = fuzzySearch(realName).size();
        }else{
            totalCount = findAll().size();
        }

        pageModel = PageUtil.createPage(currPage, realName, data, totalCount);

        return pageModel;
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public Map<String, Object> findBaseInfo(Integer id) {
        return userDao.findBaseInfo(id);
    }

    @Override
    public Map<String, Object> findAllInfo(Integer id) {
        Map<String, Object> map = userDao.findAllInfo(id);
        if(null != map) {
            map.remove("qq_openid");
            map.remove("wx_openid");
            map.remove("password");
        }
        return map;
    }

    @Override
    public int updateDetail(Integer uId, Map<String, String> params) {
        String realName = params.get("real_name");
        String phone = params.get("phone");
        String age = params.get("age");
        String gender = params.get("gender");
        String isSecret = params.get("is_secret");
        String deptName = params.get("deptName");
        Integer deptId = deptDao.findDeptByName(deptName).getId();
        User user = new User();
        user.setId(uId);
        user.setRealName(realName);
        user.setPhone(phone);
        user.setAge(Integer.valueOf(age));
        user.setGender(gender);
        user.setDeptName(deptName);
        user.setDeptId(deptId);
        if("on".equals(isSecret)){
            user.setIsSecret("0");
        }else{
            user.setIsSecret("1");
        }
        int result = userDao.update(user);
        return result;
    }

    /**
     * 关注/取关
     * @param id
     * @param userId
     * @return
     */
    @Override
    public Integer focus(Integer id, Integer userId) {
        // 查询是否有这几一条关注记录
        Map<String, Object> map = userDao.findFocusByUserIdAndFocusId(id, userId);

        if(map == null || map.isEmpty()){
            userDao.insertFocus(id, userId);
            return 1;
        }
        // 如果已经加了关注那么取消关注
        userDao.unFocus(id,userId);
        return 0;
    }

    /**
     * 根据用户id
     * 查找其所关注的所有用户的id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findFocus(Integer id) {
        return userDao.findFocus(id);
    }

    /**
     * 模糊查询
     * @param realName
     * @return
     */
    @Override
    public List<User> fuzzySearch(String realName) {
        return userDao.fuzzySearch(realName);
    }

    /**
     * 用户被查看自增
     * @param id
     */
    public void incLook(Integer id){
        userDao.incLook(id);
    }


    /**
     * 根据用户id查询其粉丝的id
     * @param id
     * @return
     */
    public Integer findFans(Integer id){
        return userDao.countByUserId(id);
    }

    @Override
    public PageEntity<Map<String, Object>> findFocusPage(Integer currPage, Integer id) {
        PageEntity<Map<String, Object>> pageModel = new PageEntity<>();
        Integer pageSize = pageModel.getPageSize();
        // 当前页数据
        List<Map<String, Object>> data = userDao.findFocusPage(id,(currPage - 1) * pageSize,pageSize);

        //数据条数
        Integer totalCount = userDao.countByUserId(id);

        pageModel = PageUtil.createFocusPage(currPage,data,totalCount);

        return pageModel;
    }

    @Override
    public User findByWxOpenid(String openid) {
        return userDao.findByWxOpenid(openid);
    }


}
